/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include ".\ssi_profiler.h"

CProfiler theProfiler;

void SSI_Start_Profiler(const char* ID){
	//theProfiler.StartTimer(ID);
}
void SSI_Stop_Profiler(const char* ID){
	//theProfiler.StopTimer(ID);
}
void SSI_Report_Profiler(const char* filename){
	return;
	FILE* FH;
	string s;
	int ctr;
	char Mess[4096];
	FH = fopen(filename, "w");
	if(!FH) return;
	for(ctr = 0; ctr < theProfiler.NumProfiles; ctr++){
		sprintf(Mess, "%s\t%d\t%d\n", theProfiler.Profiles[ctr].ID.c_str(), theProfiler.Profiles[ctr].num_calls, theProfiler.Profiles[ctr].msec/1000);
		fprintf(FH, "%s", Mess);
	}
	fclose(FH);
}



CProfiler::CProfiler(void)
: NumProfiles(0)
{
	Profiles = new ProfileItemT[1000];
}

CProfiler::~CProfiler(void)
{
	delete [] Profiles;
}

int CProfiler::StartTimer(const char* ID)
{
	ProfileItemT* aProfile;
	if(!(aProfile = GetProfile(string(ID)))){
		aProfile = AddProfile(string(ID));
	}
	if(!aProfile) return 0;
	aProfile->last_time = clock();
	aProfile->num_calls++;
	return 1;
}

int CProfiler::StopTimer(const char* ID)
{
	clock_t curr_time;
	ProfileItemT* aProfile;
	if(!(aProfile = GetProfile(string(ID)))){
		 return 0;
	}
	curr_time = clock();
	aProfile->msec += int((double)(curr_time - aProfile->last_time) / CLOCKS_PER_SEC * 1000000);

	return 1;
}

ProfileItemT* CProfiler::GetProfile(string& ID)
{
	int ctr = 0;
	for(ctr = 0; ctr < NumProfiles; ctr++){
		if(Profiles[ctr].num_calls > 0){
			if(Profiles[ctr].ID == ID){
				return &Profiles[ctr];
			}
		}else{
			return 0;
		}
	}
	return 0;
}

ProfileItemT* CProfiler::AddProfile(string& ID)
{
	Profiles[NumProfiles].ID = ID;
	Profiles[NumProfiles].num_calls = 0;
	Profiles[NumProfiles].msec = 0;
	Profiles[NumProfiles].last_time = 0;
	NumProfiles++;
	return &Profiles[NumProfiles-1];
}
